package com.j2eefast.bussiness.contract.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.bussiness.contract.entity.BusEquipmentEntity;
import com.j2eefast.bussiness.contract.entity.BusRepairEntity;
import com.j2eefast.bussiness.contract.entity.BusScrapEntity;
import com.j2eefast.bussiness.contract.mapper.BusEquipmentMapper;
import com.j2eefast.bussiness.contract.mapper.BusRepairMapper;
import com.j2eefast.bussiness.contract.mapper.BusScrapMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>维修管理</p>
 *
 * @author: summer
 * @date: 2021-05-31 14:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */

@Service
public class BusRepairService extends ServiceImpl<BusRepairMapper, BusRepairEntity> {

    @Resource
    protected BusRepairMapper busRepairMapper;

    @Resource
    protected BusEquipmentMapper busEquipmentMapper;

    /**
     * 页面分页查询
     */
    public PageUtil findPage(Map<String, Object> params) {
        String equipmentCode = (String) params.get("equipmentCode");
        String equipmentCode2 = (String) params.get("equipmentCode2");
        String equipmentName = (String) params.get("equipmentName");
        String equipmentName2 = (String) params.get("equipmentName2");
        String equipmentModel = (String) params.get("equipmentModel");
        String meteringUnit = (String) params.get("meteriingUnit");
        String repairMoney = (String) params.get("repairMoney");
        String repairStatus = (String) params.get("repairStatus");
        String faultReason = (String) params.get("faultReason");
        String remark = (String) params.get("remark");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        String beginTime2 = (String) params.get("beginTime2");
        String endTime2 = (String) params.get("endTime2");
        Page<BusRepairEntity> page = busRepairMapper.findPage(new Query<BusRepairEntity>(params).getPage(),
                StrUtil.nullToDefault(equipmentCode, ""),
                StrUtil.nullToDefault(equipmentCode2, ""),
                StrUtil.nullToDefault(equipmentName, ""),
                StrUtil.nullToDefault(equipmentName2, ""),
                StrUtil.nullToDefault(meteringUnit, ""),
                StrUtil.nullToDefault(repairMoney, ""),
                StrUtil.nullToDefault(repairStatus, ""),
                StrUtil.nullToDefault(faultReason, ""),
                StrUtil.nullToDefault(remark, ""),
                StrUtil.nullToDefault(beginTime, ""),
                StrUtil.nullToDefault(endTime, ""),
                StrUtil.nullToDefault(beginTime2, ""),
                StrUtil.nullToDefault(endTime2, ""),
                StrUtil.nullToDefault(equipmentModel, "")
        );
        return new PageUtil(page);
    }

    public List<BusRepairEntity> findList(Map<String, Object> params) {
        String useDepartment = (String) params.get("useDepartment");
        String equipmentCode = (String) params.get("equipmentCode");
        String equipmentName = (String) params.get("equipmentName");
        String equipmentModel = (String) params.get("equipmentModel");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        String startTime2 = (String) params.get("startTime2");
        String endTime2 = (String) params.get("endTime2");

        List<BusRepairEntity> page = busRepairMapper.findList(new Query<BusRepairEntity>(params).getPage(),
                StrUtil.nullToDefault(startTime, ""),
                StrUtil.nullToDefault(endTime, ""),
                StrUtil.nullToDefault(startTime2, ""),
                StrUtil.nullToDefault(endTime2, ""),
                StrUtil.nullToDefault(equipmentCode, ""),
                StrUtil.nullToDefault(equipmentName, ""),
                StrUtil.nullToDefault(equipmentModel, "")
        );
        return page;
    }




    public boolean repairSave(HttpServletRequest request, Model model) throws ParseException {
        String total_items = request.getParameter("total_items");//认领插入条数
        int equipmentId = Integer.parseInt(request.getParameter("equipmentId"));
        //int ids = Integer.parseInt(request.getParameter("id"));//认领插入条数
        ValidatorUtil.validateEntity(total_items);
        int total = Integer.valueOf(total_items);
        List<BusRepairEntity> recordList = new ArrayList<BusRepairEntity>();//更新数据放这里面
        List<BusRepairEntity> recordList1 = new ArrayList<BusRepairEntity>();//新增数据放这里
        BusRepairEntity user = new BusRepairEntity();
        BusEquipmentEntity busEquipmentEntity = busEquipmentMapper.equipmentInfo(equipmentId);

        for (int i = 1; i <= total; i++) {
            String id = "id_" + i;
            String sendTime = "sendTime_" + i;
            String repairTime = "repairTime_" + i;
            String repairMoney = "repairMoney_" + i;
            String repairStatus = "repairStatus_" + i;
            String faultReason = "faultReason_" + i;
            String remark = "remark_" + i;
            Date date = new Date();//创建一个时间对象，获取到当前的时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
            user.setSendTime(simpleDateFormat.parse(request.getParameter(sendTime)));
            user.setRepairTime(simpleDateFormat.parse(request.getParameter(repairTime)));
            user.setRepairMoney(request.getParameter(repairMoney));
            user.setRepairStatus(request.getParameter(repairStatus));
            user.setFaultReason(request.getParameter(faultReason));
            user.setRemark(request.getParameter(remark));
            user.setEquipmentId(equipmentId);
            user.setEquipmentCode(busEquipmentEntity.getEquipmentCode());
            user.setEquipmentName(busEquipmentEntity.getEquipmentName());
            user.setEquipmentModel(busEquipmentEntity.getEquipmentModel());
            user.setMeteringUnit(busEquipmentEntity.getMeteringUnit());
            if (request.getParameter(id) != "") {
                user.setId(Integer.parseInt(request.getParameter(id)));
                //先去查询是否有记录
                BusRepairEntity info = this.repairInfo(user.getId());
                if (info != null) {
                    recordList.add(user);
                } else
                    recordList1.add(user);
            } else
                recordList1.add(user);
        }

        if (recordList != null && !recordList.isEmpty()) {
            this.updateRepair(recordList);
        }
        if (recordList1 != null && !recordList1.isEmpty()) {
            return this.saveRepair(recordList1) ? true : false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    boolean saveRepair(List<BusRepairEntity> recordList) {
        return busRepairMapper.saveRepair(recordList);
    }

    @Transactional(rollbackFor = Exception.class)
    boolean updateRepair(List<BusRepairEntity> recordList) {
        return busRepairMapper.updateRepair(recordList);
    }


    public BusRepairEntity repairInfo(int id) {
        return busRepairMapper.repairInfo(id);
    }


    public List<BusRepairEntity> repairList(int id) {
        return busRepairMapper.repairList(id);
    }

    public boolean deleteByIds(Long[] ids) {

        return busRepairMapper.deleteByIds(ids);

    }

    public boolean updateCheck(Long id) {
        Long userId = UserUtils.getUserInfo().getId();

        return busRepairMapper.updateCheck(id, userId);

    }


    public int getDepreciationInfo(String equipmentCode) {

        BusRepairEntity busRepairEntity = busRepairMapper.getbusRepairInfo(equipmentCode);
        if(busRepairEntity!=null){
            return 1;
        }else
            return 0;
    }
}
